import axios, { AxiosResponse } from "axios";
import { StoreModel } from "../models/Store";
import { UserModel } from "../models/User";
import { TakeoutModel } from "../models/Takeout";

class ManagerApis {
    queryAuditingStoreList(id: string): Promise<Array<StoreModel>> {
        let promise: Promise<Array<StoreModel>> = new Promise((resolve, reject) => {
            let user: UserModel = {
                id: id
            };
            axios.get('/api/audit/audit-store', { params: user })
                .then((reslut: AxiosResponse) => {
                    const code: number = reslut.data.code;
                    if (code == 200) {
                        let storeList: Array<StoreModel> = reslut.data.data;
                        resolve(storeList);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        });
        return promise;
    }

    auditedStore(id: string, sid: string, state: number): Promise<boolean> {
        let promise: Promise<boolean> = new Promise((resolve, reject) => {
            let obj: object = {
                id: id,
                store_id: sid,
                state: state
            };
            axios.post('/api/audit/audit-store', obj)
                .then((reslut: AxiosResponse) => {
                    const code: number = reslut.data.code;
                    if (code == 200) {
                        resolve(true);
                    } else {
                        reject(false);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        });
        return promise;
    }

    queryAuditingTakeoutList(id: string): Promise<Array<TakeoutModel>> {
        let promise: Promise<Array<TakeoutModel>> = new Promise((resolve, reject) => {
            let user: UserModel = {
                id: id
            };
            axios.get('/api/audit/audit-takeout', { params: user })
                .then((reslut: AxiosResponse) => {
                    const code: number = reslut.data.code;
                    if (code == 200) {
                        let takeoutList: Array<TakeoutModel> = reslut.data.data;
                        resolve(takeoutList);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        });
        return promise;
    }


    auditedTakeout(id: string, tid: number, state: number): Promise<boolean> {
        let promise: Promise<boolean> = new Promise((resolve, reject) => {
            let obj: object = {
                id: id,
                takeout_id: tid,
                state: state
            };
            axios.post('/api/audit/audit-takeout', obj)
                .then((reslut: AxiosResponse) => {
                    const code: number = reslut.data.code;
                    if (code == 200) {
                        resolve(true);
                    } else {
                        reject(false);
                    }
                })
                .catch((error) => {
                    reject(error);
                })
        });
        return promise;
    }
}

export const managerApis: ManagerApis = new ManagerApis();